package br.com.vti.leilao.view.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import br.com.vti.leilao.model.business.Business;
import br.com.vti.leilao.model.business.LanceBusiness;
import br.com.vti.leilao.model.business.exception.ApplicationException;
import br.com.vti.leilao.model.entity.Lance;
import br.com.vti.leilao.model.entity.Leilao;
import java.util.Date;
import java.util.GregorianCalendar;

@ManagedBean
@ViewScoped
public class LanceBean extends CommonBean<Lance, ApplicationException> {

    private static final long serialVersionUID = 7053723507220511445L;
    
    @ManagedProperty("#{lanceBusiness}")
    private Business<Lance, ApplicationException> business;
    
    /* (non-Javadoc)
     * @see br.com.vti.lance.view.bean.Bean#getBusiness()
     */
    @Override
    public Business<Lance, ApplicationException> getBusiness() {
        return business;
    }

    /* (non-Javadoc)
     * @see br.com.vti.lance.view.bean.Bean#setBusiness(br.gov.ce.saude.sesacore.business.Business)
     */
    @Override
    public void setBusiness(Business<Lance, ApplicationException> business) {
        this.business = business;
    }

    private Lance consultarUltimoLance(Leilao leilao) {
        Lance lance = null;
        if (leilao != null && leilao.getId() != null && leilao.getId() > 0) {
            lance = new Lance();
            lance.setLeilaoFk(leilao);
            LanceBusiness lanceBusiness = ((LanceBusiness)getBusiness());
            lance = lanceBusiness.consultarUltimoLance(lance);
        }
        return lance;
    }
    
    public String consultarStatusLeilao(Leilao leilao) {
        String status = "";
        if (leilao != null && leilao.getId() != null && leilao.getId() > 0) {
            Date dataAtual = new Date();
            if (dataAtual.before(leilao.getDataInicio())) {
                status = "Vai começar às";
            } else {
                Lance lance = consultarUltimoLance(leilao);
                if (lance == null) {
                    status = "Já começou";
                } else {
                    Integer tempoRest = qtdTempoRestante(dataAtual, lance.getHoraLance(), leilao.getTempoDuracao());
                    if (tempoRest == 0) {
                        status = "Encerrado";
                    } else {
                        status = "Em andamento";
                    }
                }
            }
        }
        return status;
    }

    private Integer qtdTempoRestante(Date dataAtual, Date dataUltimoLance, Integer tempoDuracao) {
        Integer res = 0;
        GregorianCalendar gc1 = new GregorianCalendar();
        gc1.setTime(dataAtual);
        
        GregorianCalendar gc2 = new GregorianCalendar();
        gc2.setTime(dataUltimoLance);
        
        Integer diff = Integer.parseInt(String.valueOf((gc1.getTimeInMillis() - gc2.getTimeInMillis())/1000));
        if (diff < tempoDuracao) {
            res = tempoDuracao - diff;  
        }
        return res;
    }
    
    @Override
    public void salvar() {
        getBusiness().getMessageFactory().addErrorMessage(getBusiness().getMessageFactory().getMessage("erro.gravar.lance"));
    }
    
}